#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        int cur = 0;

        sort(nums.begin(), nums.end());

        vector<vector<int>> ret;
        // ret.resize(n);
        // for (int i = 0; i < n; i++)
        //     ret[i].resize(3);

        for (int i = 0; i < n - 2; i++)
        {
            if (i != 0 && nums[i] == nums[i - 1])
                continue;

            int left = i + 1, right = n - 1;
            while (left < right)
            {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == 0)
                {
                    // ret[cur][0] = nums[i];
                    // ret[cur][1] = nums[left];
                    // ret[cur][2] = nums[right];

                    // cur++;
                    ret.push_back(vector<int>{nums[i], nums[left], nums[right]});

                    while (left < right && nums[left] == nums[++left]);
                    while (left < right && nums[right] == nums[--right]);
                }
                else if (sum > 0)
                    //while (left < right && nums[right] == nums[--right]);
                    --right;
                else
                    //while (left < right && nums[left] == nums[++left]);
                    ++left;

            }
        }

        //ret.resize(cur);

        cout << cur << endl;

        return ret;
    }
};